package com.banciyuan.bcywebview.biz.setting.bansetting;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.fragment.BaseLazyContainerFragment;
import com.banciyuan.bcywebview.base.helper.ProgressbarHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.biz.share.block.BlockHelper;
import com.banciyuan.bcywebview.biz.share.block.IBlockHelper;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.DiscoverData;

/**
 * Created by squall on 2017/5/12.
 */

public class BlockCircleFragment extends BaseLazyContainerFragment {

    private ProgressbarHelper mProgressbarHelper;
    private View mProgressbarView;

    private List<DiscoverData> ulist = new ArrayList<>();
    private BlockCircleListAdapter blockListAdapter;
    private ListView mListView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.blocklist_fragment, null);
        initProgressbar(rootView);
        initUi(rootView);
        return rootView;
    }

    @Override
    public void fetchData() {
        initData();
    }

    @Override
    protected void initProgressbar(View rootView) {
        mProgressbarView = rootView.findViewById(R.id.base_progressbar);
        mProgressbarHelper = new ProgressbarHelper(mProgressbarView);
        mProgressbarHelper.setActionCallbacks(new ProgressbarHelper.OnProgressCallbacks() {
            @Override
            public void onActionClick(int id) {
                mProgressbarHelper.onLoading();
                initData();
            }
        });
        mProgressbarHelper.onLoading();
    }

    @Override
    protected void initUi(View rootView) {
        mListView = (ListView) rootView.findViewById(R.id.refreshview);
    }

    @Override
    protected void initData() {
        IBlockHelper iBlockHelper = new IBlockHelper() {
            @Override
            public void getSucc(String json) {
                try {
                    Gson gson = new Gson();
                    JSONObject jsonObject = new JSONObject(json);
                    List<DiscoverData> templist = gson.fromJson(jsonObject.getString(HttpUtils.UPLOAD_DATA), new TypeToken<List<DiscoverData>>() {
                    }.getType());
                    ulist.addAll(popIllegal(templist));
                    renderData();
                } catch (Exception e) {
                    e.printStackTrace();
                    mProgressbarHelper.onFailed();
                }
            }

            @Override
            public void getFail() {
                mProgressbarHelper.onFailed();
            }
        };

        BlockHelper blockHelper = new BlockHelper(iBlockHelper, getActivity());
        blockHelper.getBlockCircleList(UserDataHelper.getInstance(getActivity()).getToken());
    }

    private void renderData() {
        blockListAdapter = new BlockCircleListAdapter(getActivity(), ulist);
        mListView.setAdapter(blockListAdapter);
        mProgressbarHelper.onSuccess();
    }

    private List<DiscoverData> popIllegal(List<DiscoverData> templist) {
        List<DiscoverData> ulist = new ArrayList<>();
        for (DiscoverData u : templist) {
            if (!TextUtils.isEmpty(u.getId())) {
                ulist.add(u);
            }
        }
        return ulist;
    }
}
